package com.example.zhu95.mytimber.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.zhu95.mytimber.MyApplication;

/**
 * @author zhu95
 * @date 2018/3/28 18:49
 */
public class SharedPreferencesUtil {
    private static SharedPreferencesUtil util;
    public static final String filename = "appData";
    public static final int mode = Context.MODE_PRIVATE;
    Context context = MyApplication.getContext();

    public static final String RECORD_SECOND = "RECORD_SECOND";

    public static SharedPreferencesUtil getSharedPreferencesUtil() {
        if (util == null) {
            util = new SharedPreferencesUtil();
        }
        return util;
    }

    public void put(String key, String value) {
        SharedPreferences.Editor editor = context.getSharedPreferences(filename, mode).edit();
        editor.putString(key, value);
        editor.apply();
    }

    public void put(String key, int value) {
        SharedPreferences.Editor editor = context.getSharedPreferences(filename, mode).edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public void put(String key, boolean value) {
        SharedPreferences.Editor editor = context.getSharedPreferences(filename, mode).edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public void put(String key, Long value) {
        SharedPreferences.Editor editor = context.getSharedPreferences(filename, mode).edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public void put(String key, float value) {
        SharedPreferences.Editor editor = context.getSharedPreferences(filename, mode).edit();
        editor.putFloat(key, value);
        editor.apply();
    }

    public String getString(String key) {
        SharedPreferences preferences = context.getSharedPreferences(filename, mode);
        return preferences.getString(key, "");
    }

    public int getInt(String key) {
        SharedPreferences preferences = context.getSharedPreferences(filename, mode);
        return preferences.getInt(key, -1);
    }

    public boolean getBoolean(String key) {
        SharedPreferences preferences = context.getSharedPreferences(filename, mode);
        return preferences.getBoolean(key, false);
    }

    public long getLong(String key) {
        SharedPreferences preferences = context.getSharedPreferences(filename, mode);
        return preferences.getLong(key, -1);
    }

    public float getFloat(String key) {
        SharedPreferences preferences = context.getSharedPreferences(filename, mode);
        return preferences.getFloat(key, -1);
    }
}
